package cn.huaqingcheng.tianshu.infrastructure.sys.repository;

import cn.huaqingcheng.tianshu.core.sys.model.SysFile;
import cn.huaqingcheng.tianshu.core.sys.repository.SysFileRepository;
import cn.huaqingcheng.tianshu.infrastructure.sys.SystemConvert;
import cn.huaqingcheng.tianshu.infrastructure.sys.entity.SysFileEntity;
import cn.huaqingcheng.tianshu.infrastructure.sys.mapper.SysFileMapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * SysFileRepositoryImpl
 */
@Repository
@RequiredArgsConstructor
public class SysFileRepositoryImpl implements SysFileRepository {

    private final SysFileMapper fileMapper;


    @Override
    public SysFile save(SysFile file) {
        SysFileEntity entity = SystemConvert.INSTANCE.toEntity(file);
        fileMapper.insert(entity);
        return SystemConvert.INSTANCE.toModel(entity);
    }

    @Override
    public Optional<SysFile> selectByPath(String path) {
        return new LambdaQueryChainWrapper<>(fileMapper)
                .eq(SysFileEntity::getPath, path)
                .oneOpt()
                .map(SystemConvert.INSTANCE::toModel);
    }

}
